package cn.bonoon.kernel.web.html.form;

import static cn.bonoon.kernel.util.StringHelper.isNotEmpty;
import cn.bonoon.kernel.descriptors.UIPropertyAccessor;
import cn.bonoon.kernel.util.BoolType;
import cn.bonoon.kernel.web.annotations.form.PropertyEditor;
import cn.bonoon.kernel.web.annotations.form.PropertyInsert;
import cn.bonoon.kernel.web.annotations.form.PropertyUpdate;
import cn.bonoon.kernel.web.components.AbstractComponent;
import cn.bonoon.kernel.web.html.UIPropertyBuilder;

/**
 * <pre>
 * 生成数据查看和编辑的界面内容
 * </pre>
 * 
 * @author jackson
 * 
 */
public final class PropertyEditParser extends AbstractDetailablePropertyParser<EditableParser> implements ComponentPropertyParser{
	private Boolean readonly;
	private Boolean required;
	private String validType;
	private String missingMessage;
	private String invalidMessage;

	private String val(String s, String d) {
		return isNotEmpty(s) ? s : d;
	}

	private int val(int s, int d) {
		return s > 0 ? s : d;
	}
	
	@Override
	public void hasFile() {
		formParser.hasFile();
	}

	private Boolean toTrue(BoolType bt) {
		if (bt == BoolType.UNDEFINED)
			return null;
		return bt == BoolType.TRUE;
	}

	private PropertyEditParser(EditableParser formParser, UIPropertyAccessor accessor) {
		super(formParser, accessor);
	}

	public PropertyEditParser(EditableParser reader, UIPropertyAccessor accessor, PropertyEditor editor, PropertyUpdate modify) {
		this(reader, accessor);
		if (null != modify) {
			readonly = toTrue(modify.readonly());
			name = modify.name();
			unit = modify.unit();
			ordinal = modify.value();
			height = modify.height();
			width = modify.width();
			colspan = modify.colspan();
			rowspan = modify.rowspan();
			required = toTrue(modify.required());
			setValidType(modify.validType());
			setMissingMessage(modify.missingMessage());
			setInvalidMessage(modify.invalidMessage());
		}
		initDefault(accessor, editor);
	}

	public PropertyEditParser(
			AbstractEditableBuilder reader, 
			UIPropertyAccessor accessor,
			PropertyEditor editor, 
			PropertyInsert create) {
		this(reader, accessor);
		if (null != create) {
			readonly = toTrue(create.readonly());
			name = create.name();
			unit = create.unit();
			ordinal = create.value();
			height = create.height();
			width = create.width();
			colspan = create.colspan();
			rowspan = create.rowspan();
			required = toTrue(create.required());
			setValidType(create.validType());
			setMissingMessage(create.missingMessage());
			setInvalidMessage(create.invalidMessage());
		}
		initDefault(accessor, editor);
	}

	private void initDefault(UIPropertyAccessor accessor, PropertyEditor editor) {
		if (null != editor) {
			if (null == readonly){
				readonly = editor.readonly();
			}
			name = val(name, editor.name());
			unit = val(unit, editor.unit());
			ordinal = val(ordinal, editor.value());
			height = val(height, editor.height());
			width = val(width, editor.width());
			colspan = val(colspan, editor.colspan());
			rowspan = val(rowspan, editor.rowspan());
			if (null == required){
				required = editor.required();
			}
			validType = val(validType, editor.validType());
			setMissingMessage(val(getMissingMessage(), editor.missingMessage()));
			setInvalidMessage(val(getInvalidMessage(), editor.invalidMessage()));
		}
		init();
	}

	@Override
	protected void init() {
		super.init();
		if(width > 1){
			editorWidth = width;
		}else{
			editorWidth = realWidth - 5;
		}
//		TODO optionArray = accessor.getOption();
	}
	private int editorWidth;

	public boolean isReadonly() {
		return null != readonly && readonly;
	}

	public boolean isRequired() {
		return null != required && required;
	}

	@Override
	protected void buildContent(UIPropertyBuilder pb) throws Exception {
		StringBuilder html = pb.getInitHtml();
		if (isReadonly()) {
			html.append("$!{").append(pb.getBindedName()).append('.').append(accessor.getName()).append('}');
		} else {
			// 查看是否有定义组件
			// 组件的原则是，先处理普通的，再处理easyui组件，最后处理其它组件（如：在线编辑器等...）
			addHelper(accessor);
			AbstractComponent.get(this, accessor).build(pb);
			// 显示性填项的标志
			if (isRequired()) html.append("<span class='request'>*</span>");
		}
	}

	@Override
	protected void calculateHelperWidth(int s) {
		if(width < 1){
			if (s == 0) {
				editorWidth -= imageSize;
			} else {
				editorWidth -= ((s + 2) * fontSize);
			}
		}
	}

	public int getEditorwidth() {
		return editorWidth;
	}

	public String getValidType() {
		return validType;
	}

	public void setValidType(String validType) {
		this.validType = validType;
	}

	public String getMissingMessage() {
		return missingMessage;
	}

	public void setMissingMessage(String missingMessage) {
		this.missingMessage = missingMessage;
	}

	public String getInvalidMessage() {
		return invalidMessage;
	}

	public void setInvalidMessage(String invalidMessage) {
		this.invalidMessage = invalidMessage;
	}
}
